<?php

define("COOKIE_NAME", "rsg");

class Admin {

        private $name;
        private $password;
        private $loggedIn;

        function __construct($name, $password) {
                $this->name = $name;
                $this->password = $password;
                $this->loggedIn = $this->checkCookie();

        }

        function login($given_name, $given_password) {
                $valid = strcmp($given_name, $this->name) == 0 && strcmp($given_password, $this->password)==0;
                if($valid) {
                        $this->loggedIn = $valid;
                        $this->setPersistentLogin();
                }
                return $valid;
        }

        function checkCookie() {
                $found = isset($_COOKIE[COOKIE_NAME]) && $_COOKIE[COOKIE_NAME]==$this->name.''.$this->encryptPassword($this->password);
                return $found;

        }

        function setPersistentLogin() {
                $result = setcookie(COOKIE_NAME, $this->name.''.$this->encryptPassword($this->password));
        }

        function isLoggedIn() {
                return $this->loggedIn;
        }

        function encryptPassword($password) {
                return sha1($password);
        }
}
?>
